#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from PIL import Image
# open an image file (.jpg or.png) you have in the working folder
im1 = Image.open("lenaFull.jpg")
print(im1.format, im1.size, im1.mode)

#裁剪 4-tuple(left, upper, right, lower) 左上角为(0 ,0)
#box = (100, 100, 200, 200)
#region = im1.crop(box)
#region.show()
#旋转
#region = region.transpose(Image.ROTATE_180)
#region.show()
#粘贴
#im1.paste(region, box)
#im1.show()

#分离通道 对于单通道图片，split()返回图像本身。为了处理单通道图片，必须先将图片转成RGB。
r, g, b = im1.split()
print(r.format, r.size, r.mode)
print(g.format, g.size, g.mode)
print(b.format, b.size, b.mode)

#操作像素 提高50%对比度
average = 128
r = r.point(lambda p: (average + (p - average) * (1 + 0.5) <= 0 and 0) or (average + (p - average) * (1 + 0.5) >= 255 and 255) or (average + (p - average) * (1 + 0.5)))
g = g.point(lambda p: (average + (p - average) * (1 + 0.5) <= 0 and 0) or (average + (p - average) * (1 + 0.5) >= 255 and 255) or (average + (p - average) * (1 + 0.5)))
b = b.point(lambda p: (average + (p - average) * (1 + 0.5) <= 0 and 0) or (average + (p - average) * (1 + 0.5) >= 255 and 255) or (average + (p - average) * (1 + 0.5)))

#合并通道
im2 = Image.merge('RGB', (r, g, b))
im2.show()

#保存
im2.save("highContrast.jpg")

if __name__ == '__main__':
	pass